<?php
require_once("includes/include.php");
$Helper= new Helper();
$Log= new Log();
$post = $_POST;
if (!empty($post)&&$post["postType"]==0){
	//创建事件
	$dao = new eventdao();
	$name = $Helper->formDataFilter($post["name"]);
	$des = $Helper->formDataFilter($post["des"]);
	$start = $post["startD"];
	$end = $post["endD"];
	@session_start();
	$userId = $_SESSION["userId"];
	date_default_timezone_set("Asia/shanghai");
	$createD = date("Y-m-d H:i:s");
	$createEvent = $dao->createEvent($name,$des,$start,$end,$userId,$createD);
	$eventId = $dao->getEventId($createD,$userId);
	$updateRootFolder = $dao->updateRootEventDir($eventId);
	$newEvent = $dao->getTargetEventInfo($eventId);
	
	if (!empty($newEvent)){
		foreach ($newEvent as $event){
			echo "<div id='eventnumber_$event[id]' style='float:left;margin-left:10px;width:20px;overflow:hidden;font-size:18px;font-weight:bolder;color:purple;margin-right:-5px;margin-top:-3px;'>$event[id]</div>";
	 		echo "<div class='eventIterm' id='eventId_$event[id]'>";
	 		echo "<div class='eventName'><a onclick=\"getEventDetail('$event[id]',this)\">$event[name]</a></div>";
	 		echo "<div class='eventDescription'>描述：$event[description]</div>";
	 		echo "<div class='eventCreatD'>创建日期：$event[createDate]</div>";
	 	    echo "<div class='eventStartD'>开始日期：$event[startDate]</div>";
	 		echo "<div class='eventEndD'>结束日期：$event[endDate]</div>";
	 		echo "</div>";
		}
	}
}else if (!empty($post)&&$post["postType"]==1){
	//get target event information
	$dao= new eventdao();
	$eventInfo = $dao->getTargetEventInfo($post['eventId']);
	if(!empty($eventInfo)){
		foreach ($eventInfo as $info){
			echo "<div class='eInfoId'>编号：".$info['id']."</div>";
			echo "<div class='eInfoNmae'>名称：".$info['name']."</div>";
			echo "<div class='eInfoDcr'>描述：".$info['description']."</div>";
			echo "<div class='eInfoCd'>创建时间：".$info['createDate']."</div>";
			echo "<div class='eInfoSt'>开始时间：".$info['startDate']."</div>";
			echo "<div class='eInfoEt'>结束时间：".$info['endDate']."</div>";
			$fileFolder = $info['fileFolder'];
			$fileDiv = getFileByDbFolder($fileFolder);
			if ($fileDiv){
				$hasFile = "文件：";
			}
			echo "<div class='eInfoFiles'>$hasFile"."$fileDiv</div>";
		}
	}
	
}else if (!empty($post)&&$post["postType"]==2){
	//删除文件
	$parth= $post["parth"];
	$dao = new eventdao();
	$deleteFile = $dao->deleteFileByParth($parth);
	if($deleteFile)
	echo "success";
	else $Log->write("can't delete data in the db with file parth:".$parth);
	if (file_exists($parth)){
		$delete = unlink($parth);
	}else{
		$Log->write("文件不存在");
	}
}else if (!empty($post)&&$post["postType"]==3){
	//新增文件后更新文件列表
	$eventID = $post["eventId"];
	$fileDiv = getFileByDbFolder($eventID);
	if ($fileDiv){
		$hasFile = "文件：";
	}
	echo $hasFile.$fileDiv;
}else if (!empty($post)&&$post["postType"]==4){
	//删除事件
	$dao = new eventdao();
	$eventID = $post["eventId"];
	$delete = $dao->deleteEvent($eventID);
	if($delete){
		$baseFolder = getEventBaseFolder($eventID);
		if(is_dir($baseFolder)){
			$dir = opendir($baseFolder);
			while ($file = readdir($dir)){
				if($file!="."&&$file!=".."){
					$fileparth = $baseFolder."/".$file;
					if (file_exists($fileparth)){
						unlink($fileparth);
						
					}else{
						$Log->write("file doesn't exist:file=>".$fileparth);
					}
				}
			}
			closedir($dir);
			$baseFolder=$baseFolder."/";
			$rm = rmdir($baseFolder);
			if (!$rm){
				$Log->write("不能删除目录：".$baseFolder);
			}
		}
	}
	echo "success";
}

function getFileFromFolder($folder){
	$fileDiv = "";
	global $Helper,$Log;
	$baseDir = $Helper->getEventBaseFolder();
	$tempFolder = $baseDir.$folder;
	if(is_dir($tempFolder)){
		$dir = opendir($tempFolder);
		if ($dir){
			while($file = readdir($dir)){
				if($file=="."||$file==".."){
					continue;
				}
				$fileserver = $file;
				$sub = strrchr($file,".");
				$beforePart = str_replace($sub,"",$file);
				$file = base64_decode($beforePart).$sub;
				$fileDiv .= "<div class='fileIterm' onmouseover='displayFileControl(this)' onmouseout='hiddenFileControl(this)'><a href='#' onclick=\"downloadFile('$tempFolder/$fileserver')\">$file</a>
				<a href='#' style='margin-left:25px;' class='fileManageA' onclick=\"downloadFile('$tempFolder/$fileserver')\"><img src='style/sort-arrow-down.png' />下载</a>
				<a href='#' style='margin-left:25px;' class='fileManageA' onclick=\"deleteEventFile('$tempFolder/$fileserver',this)\">删除</a></div>";
			}
			closedir($dir);
		}else $Log->write("Failed open dir:".$tempFolder);
	}
	return $fileDiv;
}
function getEventBaseFolder($eventId){
	global $Helper,$Log;
	$baseDir = $Helper->getEventBaseFolder();
	$tempFolder = $baseDir.$eventId;
	return $tempFolder;
}
function getFileByDbFolder($folderId){
	$dao  = new eventdao();
	$files = $dao->getFileByDbFolder($folderId);
	if (empty($files))
	return false;
	foreach ($files as $file){
		$parth = $file["parth"];
		$name  = $file["name"];
		$fileDiv .= "<div class='fileIterm' onmouseover='displayFileControl(this)' onmouseout='hiddenFileControl(this)'><img src='style/fam_page_white.gif' /><a href='#' onclick=\"downloadFile('$parth')\">$name</a>
				<a href='#' style='margin-left:25px;' class='fileManageA' onclick=\"downloadFile('$parth')\"><img src='style/sort-arrow-down.png' />下载</a>
				<a href='#' style='margin-left:25px;' class='fileManageA' onclick=\"deleteEventFile('$parth',this)\"><img src='style/no.png' />删除</a></div>";
		
	}
	return $fileDiv;
}